package com.data.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.ChargeConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.ChargeConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.BaseDataDao;
import com.data.dao.ChargeMinDao;
import com.data.entity.BaseData;
import com.data.entity.ChargeMin;

@Service("chargeMinService")
public class ChargeMinServiceImpl implements ChargeMinService{ 
	@Resource(name="baseDataDao")
	private BaseDataDao baseDataDao;
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="chargeConfigDao")
	private ChargeConfigDao chargeConfigDao;
	@Resource(name="chargeMinDao")
	private ChargeMinDao chargeMinDao;
	/**
	 * 保存生产线下所有设备电费半小时统计表数据
	 * @param belongTime
	 */
	public void saveChargeMin(Date belongTime){
		List<LineConfig> lines=lineConfigDao.findAllLine();
		if(lines.size()!=0){
			for(LineConfig line:lines){
			//	this.saveLineChargeMin(line.getId(), belongTime);  生产线不提取数据
    			List<Equipment> equs=equipmentDao.listByLine(line.getId());
    			if(equs.size()!=0){
    				for(Equipment e:equs){
    					this.saveEquChargeMin(e.getId(),belongTime);
    			    }
    			 }
    		  }
    	 }
	}
	
	/**
	 * 提取生产线半小时电费方法
	 * @param lineId
	 * @param belongTime
	 */
//	private void saveLineChargeMin(int lineId,Date belongTime){
//		SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		LineConfig line=lineConfigDao.get(lineId);
//		//查询半小时记录
//		BaseData bd=baseDataDao.getBaseDataByEquNoAndDate(line.getLinesNo(), sdf.format(belongTime));
//		if(bd!=null){
//			ChargeConfig config= chargeConfigDao.findByTime(belongTime);
//			if(config!=null){
//				//当前时间是否保存此记录
//				ChargeMin min=chargeMinDao.getChargeMinByIdAndDate(lineId, sdf.format(belongTime),"Y");
//				boolean isNew=false;
//				if(min==null){
//					min=new ChargeMin();
//					min.setLineId(lineId);
//					min.setChargeName(config.getChargeName());
//					min.setBelongTime(belongTime);
//					isNew=true;
//				}
//				//查询时间前半小时记录
//				String before=sdf.format(new Date(belongTime.getTime()-30*60*1000));
//				BaseData beforebd=baseDataDao.getBaseDataByEquNoAndDate(line.getLinesNo(), before);
//				double relativeComp=0.0;double charges=0.0;
//				if(beforebd!=null){
//					relativeComp=bd.getP_engry()-beforebd.getP_engry();
//					charges=relativeComp*config.getUnitValue();
//				}else {
//					relativeComp=bd.getP_engry();
//					charges=relativeComp*config.getUnitValue();
//				}
//				min.setRelativeComp(relativeComp);
//				min.setCharge(charges);
//				if(isNew){
//					if(relativeComp!=0.0){
//						if(sdft.format(bd.getTime()).equals(sdft.format(beforebd.getTime()))){
//							chargeMinDao.save(min);
//						}
//					}
//				}else{
//					chargeMinDao.update(min);
//				}
//			}
//		}
//	}
	/**
	 * 提取设备半小时电费方法
	 * @param equId
	 * @param belongTime
	 */
	private void saveEquChargeMin(int equId,Date belongTime){
		SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Equipment equ=equipmentDao.get(equId);
		//查询半小时记录
		BaseData bd=baseDataDao.getBaseDataByEquNoAndDate(equ.getEquNo(), sdf.format(belongTime));
		if(bd!=null){
			ChargeConfig config= chargeConfigDao.findByTime(belongTime);
			if(config!=null){
				//当前时间是否保存此记录
				ChargeMin min=chargeMinDao.getChargeMinByIdAndDate(equId, sdf.format(belongTime),"N");
				boolean isNew=false;
				if(min==null){
					min=new ChargeMin();
					min.setEquId(equId);
					min.setChargeName(config.getChargeName());
					min.setBelongTime(belongTime);
					isNew=true;
				}
				//查询时间前半小时记录
				String before=sdf.format(new Date(belongTime.getTime()-30*60*1000));
				BaseData beforebd=baseDataDao.getBaseDataByEquNoAndDate(equ.getEquNo(), before);
				double relativeComp=0.0;double charges=0.0;
				if(beforebd!=null){
					relativeComp=bd.getP_engry()-beforebd.getP_engry();
					charges=relativeComp*config.getUnitValue();
				}
//				else{
//					relativeComp=bd.getP_engry();
//					charges=relativeComp*config.getUnitValue();
//				}
				min.setRelativeComp(relativeComp);
				min.setCharge(charges);
				if(isNew){
					if(relativeComp>=0.0 && relativeComp<100){
						if(sdft.format(bd.getTime()).equals(sdft.format(beforebd.getTime()))){
				        	chargeMinDao.save(min);
						}
					}
				}else{
					chargeMinDao.update(min);
				}
			}
		}
	}
}
